/*
 * 现金单付
 * 2018-03-23
 */
;(function ($) {
	//现金单付入口
	$.fn.spa_posorderAlonePay=function(options){
    	//创建对象
		var $ele = new alonePay(this, options);
		//入口
		$ele.getDefault();
	}
	
	//构造函数
    var alonePay = function(ele, opt) {   	
    	//选择器对象    	
        this.$element=ele;   
        //默认参数
        this.defaults = {
    		no:"",//no号
    		bh:"",//bh
			mc:"",//mc
			type:"",//支付类型
			price:"",//金额
			bz:"",//备注
			method:"",//支付类型
			odata:{},//其他特有数据
			arg : {},
			operation:"",
			nos_checkout:"",
    		getDefaultBack:function(){},//获取默认值完毕回调函数
        	workComplete:function(){},//插件加载完成
    		dataBack:function(){},//完成结账回调函数
    		returnBack:function(){},//返回回调函数
        },       
        //初始化参数
        this.options = $.extend({}, this.defaults, opt);
    }

    //属性方法
    alonePay.prototype = {
    	//临时变量
		defaultValue:null,
    	//获取默认值
    	getDefault:function(){
    		var $t=this;
    		var $opt=$t.options;
    		var postData={};
    		postData.arg = $.extend($opt.arg,$opt.type);
    		postData.ct_brzd = {"no":$opt.no};
    		postData.ct_brxsxm = {"bh":$opt.bh,"mc":$opt.mc,"dfje":$opt.price,"bz":$opt.bz};
    		if($opt.method == "AR"){
    			postData.ho_arledger = $opt.odata;
    		}else if($opt.method == "RM"){
    			postData.ho_gledger = $opt.odata;
    		}
//    		postData.arg = $opt.arg;
    		var procedure = "SPAOrderPayment";
    		if($opt.isTogather){
//    			procedure = "POSOrderPayment_Checkout_";
    			postData.arg.nos = $opt.nos;
    		}
    		postData.arg.nos_checkout = $opt.nos_checkout;
    		postData.arg.operation = $opt.operation;
    		console.log(JSON.stringify(postData))
//    		debugger
    		if($opt.operation=="选择结账"){
    			postData.arg.xhs = $opt.xhs;
    			$.ajax({
        			url: $.HSAPI_AJAXURL+"/qms/mobile/procedure2",      
        			type: "post",
        			dataType: "json",
        			data: $.HSAPI_ASKJSONINFO(JSON.stringify(postData),"SPAOrderPayment_Temp","{}"),
        		}).success(function (data) {
    				if(data.RETURN >= 0){
    					$opt.getDefaultBack(data);
    				}else if(data.RETURN == -10){
    					$t.errorContent(data);
    				}else{
    					jf_alert(data.MSG);
    					$opt.returnBack();
    				}
        		}).error(function (data){
        			jf_alert(data);
        			$opt.returnBack();
        		});
    		}else{
    			$.ajax({
        			url: $.HSAPI_AJAXURL+"/qms/mobile/procedure3",
        			type: "post",
        			dataType: "json",
        			data: $.HSAPI_ASKJSONINFO(JSON.stringify(postData),procedure,"{}",""),
        		}).success(function (data) {
    				if(data.RETURN >= 0){
    					$opt.getDefaultBack(data);
    				}else if(data.RETURN == -10){
    					$t.errorContent(data);
    				}else{
    					jf_alert(data.MSG);
    					$opt.returnBack();
    				}
        		}).error(function (data){
        			jf_alert(data);
        			$opt.returnBack();
        		});
    		}
//    		console.log(procedure)
//    		return false;
    		
		},
		//返回筛选器对象,用于链式操作
    	hsOle: function() {
            return this.$element;      
        },
        //出错提示
    	errorContent : function(data){
    		
    		/*------------------------------------------------*/
    		
    		var $t=this;
    		var $opt=$t.options;
    		
    		var MSG = data.MSG;
    		var name = data.checkbox;
    		var value = (data.checkboxvalue||"");
    		
    		var checked = "";
    		if(value==1){
    			checked = "checked"
    		}
    		
    		var html = 	'<h3 class="header smaller lighter blue">'+
    						'<i class="ace-icon fa fa-bullhorn"></i>'+MSG+
    					'</h3>'+
    					'<div class="alert alert-block alert-danger">'+
    						'<label>'+
    							'<input id="checkin_cccheckbox" type="checkbox" name="'+name+'" value="" class="ace" '+checked+'/>'+
    							'<span class="lbl" style="color:#478fca;">'+MSG+'</span>'+
    						'</label>'+
    					'</div>';

    		bootbox.dialog({
    			message: html,
    			buttons:{
    				"button1":{
    					"label" : $.i18n.propJsp("确定"),
    					"className" : "btn-sm btn-info",
    					"callback" : function(){
    						if($("#checkin_cccheckbox").prop("checked")){
    							$opt.arg[name]="yes";
    							$t.getDefault();
    						}else{
    							$opt.arg[name]="no";
    							$t.getDefault();
    						}
    					}
    				},	
    				"button" :
    				{
    					"label" : $.i18n.propJsp("取消"),
    					"className" : "btn-sm",
    					"callback" : function(){
    						$opt.returnBack();
    					}
    				},
    			}
    		});
    	},
    }
})(jQuery);